package com.epfresh.supplier.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.imageaware.ImageViewAware;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.OrderJoinListBean;
import com.epfresh.supplier.entity.OrderListBean;

import java.util.List;

/**
 * Created by zzq on 2016/7/19.
 */
public class OrderJoinAdapter extends BaseAdapter implements View.OnClickListener {

    public ListView lv;
    public Context context;
    public List<OrderJoinListBean> list;
    int type;
    private AdapterView.OnItemClickListener onItemClickListener;

    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public OrderJoinAdapter(Context context, ListView lv, List<OrderJoinListBean> groupList, int type) {
        this.context = context;
        this.list = groupList;
        this.lv = lv;
        this.type = type;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int groupPosition, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_order_more, null, false);
            vh.tv_order_id = (TextView) convertView.findViewById(R.id.tv_order_id);

            vh.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            vh.tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
            vh.tv_freight_end = (TextView) convertView.findViewById(R.id.tv_freight_end);

            vh.tv_post_type = (TextView) convertView.findViewById(R.id.tv_post_type);
            vh.tv_place_time = (TextView) convertView.findViewById(R.id.tv_place_time);
            vh.iv_logo = (ImageView) convertView.findViewById(R.id.iv_logo);
            vh.iv_promotion = (ImageView) convertView.findViewById(R.id.iv_promotion);
            vh.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
            vh.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            vh.ll_images = (LinearLayout) convertView.findViewById(R.id.ll_images);
            vh.rl_more = convertView.findViewById(R.id.rl_more);
            vh.rl_only_one = convertView.findViewById(R.id.rl_only_one);
            vh.tv_code = (TextView) convertView.findViewById(R.id.tv_code);
            vh.tv_logistics = (TextView) convertView.findViewById(R.id.tv_logistics);
            vh.tv_sure = (TextView) convertView.findViewById(R.id.tv_sure);
            vh.tv_more = (TextView) convertView.findViewById(R.id.tv_more);

            vh.tv_total_kind = (TextView) convertView.findViewById(R.id.tv_total_kind);
            vh.tv_total_money = (TextView) convertView.findViewById(R.id.tv_total_money);
            vh.ll_order = convertView.findViewById(R.id.ll_order);


            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        vh.tv_code.setTag(R.id.item_key_position, groupPosition);
        vh.tv_logistics.setTag(R.id.item_key_position, groupPosition);
        vh.tv_sure.setTag(R.id.item_key_position, groupPosition);

        vh.ll_order.setTag(R.id.item_key_position, groupPosition);
        vh.ll_order.setOnClickListener(this);
        vh.tv_phone.setTag(R.id.item_key_position, groupPosition);
        vh.tv_phone.setOnClickListener(this);

        OrderJoinListBean order = list.get(groupPosition);
        vh.tv_order_id.setText("订单：" + order.getNumber());
        vh.tv_place_time.setText(order.getCreateTime());
///*        if(order.getSource() == 0){
//            vh.tv_freight_end.setText("("+"含运费："+order.getProductExpressFee()+"元)");
//            vh.tv_freight_end.setVisibility(View.VISIBLE);
//        }else {
//            vh.tv_freight_end.setVisibility(View.GONE);
//        }*/
//
        vh.tv_name.setText(order.getMerchantName());
        //  vh.tv_phone.setText(order.getReceiverPhone());
        vh.tv_post_type.setText(order.getStatus());////
        vh.tv_total_kind.setText("共" + order.getProductCount() + "件");
        vh.tv_total_money.setText("订单金额：" + order.getAmount() + "元");
        vh.ll_images.setTag(R.id.item_key_position, groupPosition);
        vh.ll_images.setOnClickListener(this);
        vh.rl_more.setTag(R.id.item_key_position, groupPosition);
        vh.rl_more.setOnClickListener(this);
        if (order.getProduct().size() > 1) {
            vh.rl_more.setVisibility(View.VISIBLE);
            vh.rl_only_one.setVisibility(View.GONE);
            addImages(vh.ll_images, list.get(groupPosition));
        } else {
            vh.rl_more.setVisibility(View.GONE);
            vh.rl_only_one.setVisibility(View.VISIBLE);
//            if (2==(order.getSalesType())) {//预售
//                vh.tv_type.setVisibility(View.VISIBLE);
//                String html="<font color=\"#00ffffff\">【预售】</font>"+order.getResult().get(0).getProductTitle();
//                vh.tv_title.setText(Html.fromHtml(html));
//            } else {//现售
//                vh.tv_type.setVisibility(View.GONE);
//                vh.tv_title.setText(order.getProduct().get(0).getProductTitle());
//            }
            vh.tv_title.setText(order.getProduct().get(0).getTitle());
            ImageLoader.getInstance().displayImage(order.getProduct().get(0).getPath(), vh.iv_logo);
            /**
             * 隐藏图片左上方图片
             * */
//            String promotionMarkUrl = order.getProduct().get(0).getPath();
//            ImageLoader.getInstance().displayImage(promotionMarkUrl, new ImageViewAware(vh.iv_promotion), BaseApplication.getTagOptions(promotionMarkUrl), null, null, null);

        }

        //  initBottom(list.get(groupPosition).getButtonTypex(),vh);

        convertView.setTag(vh);
        convertView.setClickable(true);
        return convertView;
    }

    public class ViewHolder {
        TextView tv_freight_end;


        TextView tv_name, tv_phone;
        TextView tv_post_type;
        TextView tv_place_time;
        TextView tv_order_id;

        TextView tv_total_kind;
        TextView tv_total_money;
        ImageView iv_promotion;

        ImageView iv_logo;
        TextView tv_type;
        TextView tv_title;
        LinearLayout ll_images;
        View rl_more, rl_only_one;
        View ll_order;

        TextView tv_code;
        TextView tv_logistics;
        TextView tv_sure;
        TextView tv_more;
    }


    void addImages(LinearLayout v, OrderJoinListBean imagesList) {
        v.removeAllViews();
        v.setMinimumWidth(LocalDisplay.SCREEN_WIDTH_PIXELS - LocalDisplay.dp2px(30));
        for (int i = 0; i < imagesList.getProduct().size(); i++) {
            ImageView flag = new ImageView(context);
            flag.setScaleType(ImageView.ScaleType.CENTER_CROP);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LocalDisplay.dp2px(50), LocalDisplay.dp2px(50));
            flag.setLayoutParams(lp);
            LinearLayout.LayoutParams lpFrame = new LinearLayout.LayoutParams(LocalDisplay.dp2px(50), LocalDisplay.dp2px(50));
            ImageLoader.getInstance().displayImage(imagesList.getProduct().get(i % imagesList.getProduct().size()).getPath(), flag);
            FrameLayout f = new FrameLayout(context);
            lpFrame.setMargins(LocalDisplay.dp2px(7), 0, LocalDisplay.dp2px(7), 0);
            if (i == 0) {
                lpFrame.setMargins(LocalDisplay.dp2px(10), 0, LocalDisplay.dp2px(7), 0);
            }
            if (i == imagesList.getProduct().size()) {
                lpFrame.setMargins(LocalDisplay.dp2px(7), 0, LocalDisplay.dp2px(10), 0);
            }
            f.setLayoutParams(lpFrame);
            f.addView(flag);
            /**
             * 隐藏图片左上方图片
             * */
//            ImageView tag = new ImageView(context);
//            tag.setScaleType(ImageView.ScaleType.CENTER_CROP);
//            LinearLayout.LayoutParams tagLp = new LinearLayout.LayoutParams(LocalDisplay.dp2px(30), LocalDisplay.dp2px(33));
//            tag.setLayoutParams(tagLp);
//            ImageLoader.getInstance().displayImage(imagesList.getProduct().get(i % imagesList.getProduct().size()).getPath(), new ImageViewAware(tag), BaseApplication.getTagOptions(imagesList.getProduct().get(i % imagesList.getProduct().size()).getPath()), null, null, null);
//            f.addView(tag);
            v.addView(f);
        }
    }

    void initBottom(int type, ViewHolder vh) {
        vh.tv_code.setOnClickListener(this);
        vh.tv_logistics.setOnClickListener(this);
        vh.tv_sure.setOnClickListener(this);
        vh.tv_code.setVisibility(View.GONE);
        vh.tv_logistics.setVisibility(View.GONE);
        vh.tv_sure.setVisibility(View.GONE);
        switch (type) {
            case 98://备货中
                vh.tv_logistics.setVisibility(View.VISIBLE);
                vh.tv_sure.setVisibility(View.VISIBLE);
                break;
            case 97://待发货
                vh.tv_code.setVisibility(View.VISIBLE);
                vh.tv_logistics.setVisibility(View.VISIBLE);
                break;
            case 96://待收货
                vh.tv_logistics.setVisibility(View.VISIBLE);
                break;
 /*             case 95:
                vh.tv_logistics.setVisibility(View.VISIBLE);
                break;*/
        }
    }


    @Override
    public void onClick(View v) {
        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            int position = (int) tag;
// 交货码，查看物流，取消订单，确认收货，支付， 详情
            onItemClickListener.onItemClick(null, v, position, position);
        }
    }
}
